<template>
    <section>
        <!--列表展示-->
        <el-table :data="roles" highlight-current-row v-loading="listLoading"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index">
            </el-table-column>
            <el-table-column prop="name" label="角色名" sortable>
            </el-table-column>
            <el-table-column prop="sn" label="描述" sortable>
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">分配权限</el-button>
                </template>
            </el-table-column>
        </el-table>


        <!--添加修改弹出框-->
        <el-dialog :title="title" :visible.sync="dialogFormVisible">

            <el-transfer v-model="value" :data="data"></el-transfer>
            <el-button type="success" @click="sumbit">授权</el-button>

        </el-dialog>
        <!--工具条-->
        <el-col :span="24" class="toolbar">


            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>


    </section>
</template>

<script>

    export default {
        data() {
            const generateData = _ => {
                const data = [
                    {key: 26, label: '字典管理'},
                    {key: 27, label: '部门管理'},
                    {key: 28, label: '审理管理'},
                    {key: 29, label: '产品管理'},
                    {key: 30, label: '寻主管理'},
                    {key: 31, label: '已处理寻主'},
                    {key: 32, label: '宠物管理'},
                    {key: 33, label: '授权管理'}
                ];
                return data;
            };

            return {
                id: "",
                data: generateData(),
                value: [26],
                rolename: "",
                formLabelWidth: "",
                //是否打开对话框
                dialogFormVisible: false,
                title: "",
                //选中多少行
                sels: "",
                //部门列表信息展示
                roles: [],
                localPage: 1,
                pageSize: 5,
                total: 0,
                //页面加载转圈圈
                listLoading: false,


            }
        },
        methods: {
            //当前页改变
            handleCurrentChange(val) {
                this.localPage = val
                this.loadRoles()
            },
            sumbit() {
                console.log(this.value)
                let param = {
                    "ids": this.value,
                    "id": this.id
                }
                this.$http.post("/role/za" , param).then(res => {
                    console.log(res);
                    let {message, success} = res.data; //解构函数
                    if (success) {
                        this.$message({
                            message: message,
                            type: 'success'
                        });
                        this.loadData()
                    }
                })
                this.dialogFormVisible = false
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.dialogFormVisible = true;
                // console.log(row)
                this.$http.post("/role/queryPermis/" + row.id).then(res => {
                    let arr=[]
                    for (let i in res.data) {
                        arr.push(res.data[i])
                    }
                    // console.log(arr)
                    this.value = arr
                    // console.log(typeof res.data)
                   // console.log(this.value)
                })
                this.id = row.id

            },

            // 加载部门列表,并切分页
            loadRoles() {
                // console.log("aaaaa")
                let param = {
                    "localPage": this.localPage,
                    "pageSize": this.pageSize,
                }
                this.$http.post("/role/queryRoles", param).then(res => {
                    console.log(res)
                    let {data, totals} = res.data
                    this.roles = data
                    this.total = totals
                }).catch()
            },


        }, mounted() {
            this.loadRoles()
        }

    }


</script>
<style scoped>

</style>